<?php
/**
* @desc 登录表单处理
* @author kevin.wang
* @date 2015-01-01 12:00:00
**/
class RechargeForm extends BaseForm
{
	//商户订单号
	private $out_trade_no;
	//消费金额
	public $price;
	public $user_id;
	public $order_sn;
	//表单规则
	public function rules()
	{
		return array(
			array('price, user_id, order_sn', 'required', 'message'=>'价格不能为空'),
			array('price', 'numerical', 'message'=>'价格无效')
		);
	}
	//充值所需信息
	public function getRechargeData(){
		$price = $this->price;
		$alipay_config = Yii::app()->params['alipay'];
		$parameter = array(
				"service" => $alipay_config['service'],
				"partner" => $alipay_config['partner'],
				"payment_type" => $alipay_config['payment_type'],
				"notify_url" => $alipay_config['notify_url'],
				"return_url" => $alipay_config['return_url'],
				"seller_email" => $alipay_config['seller_email'],
				"out_trade_no" => $this->out_trade_no,
				"subject" => "私家之旅消费金额 {$price} 元",
				"price"	=> $price,
				"quantity" => '1',
				"logistics_fee"	=> "0.00",
				"logistics_type" => "EXPRESS",
				"logistics_payment"	=> "SELLER_PAY",
				"body" => "私家之旅消费金额 {$price} 元",
				"_input_charset" => $alipay_config['input_charset']
		);
		return $parameter;
	}
	//写入充值记录
	public function writeRechargeLog(){
		$this->out_trade_no = $this->getOrderSn();
		$data = array(
			'user_id' => $this->user_id,
			'order_sn' => $this->order_sn,
			'trade_id' => $this->out_trade_no,
			'amount' => $this->price
			);
		$model = new Recharge;
		return $model->insertRecharge($data);
	}
	//处理充值记录
	public function handleRechargeLog($status, $tradeId){
		$model = new Recharge;
		return $model->updateRechargeStatus($status, $tradeId);
	}
}
